﻿using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using Microsoft.Phone.Controls.LocalizedResources;
using Microsoft.Phone.Shell;
////
using GalaSoft.MvvmLight.Command;
using GalaSoft.MvvmLight.Messaging;
using GalaSoft.MvvmLight;
using GalaSoft.MvvmLight.Threading;
using ScheduleMVVM.Model.RuntimeFramework;
using ScheduleMVVM.Model.PersistentFramework;
using ScheduleMVVM.Localization;

namespace ScheduleMVVM.ViewModel
{
    public class RouteEntryWrapper : ViewModelBase
    {
        #region Binded properties
        public RouteEntry InnerValue
        { get; protected set; }
        public String Title
        {
            get
            {
                if (InnerValue.IsSearch) return GeneralResource.SearchResultString;
                return InnerValue.Title;
            }
        }
        public String ArrivalStationTitle
        {
            get
            {
                return InnerValue.endStation.Title;
            }
        }
        public String DepartureStationTitle
        {
            get
            {
                return InnerValue.startStation.Title;
            }
        }
        public String RouteString
        {
            get
            {
                return DepartureStationTitle + " - " + ArrivalStationTitle;
            }
        }
        public RelayCommand DeleteFavouriteCommand
        {
            get
            {
                return new RelayCommand(delegate
                {
                    Messenger.Default.Send<CancelDownloadCommand>(new CancelDownloadCommand());
                    Messenger.Default.Send<UpdateContextCommand>(new UpdateContextCommand()
                    {
                        Action = UpdateContextCommandType.DELETE,
                        Argument = this.InnerValue
                    });
                });
            }
        }
        public RelayCommand EditFavouriteCommand
        {
            get
            {
                return new RelayCommand(delegate
                {
                    Messenger.Default.Send<UpdateRouteViewCommand>(new UpdateRouteViewCommand(this));
                    ((App)App.Current).RootFrame.Navigate(new Uri("/AddFavouritePage.xaml", UriKind.Relative));
                });
            }
        }
        public RelayCommand RoutePageShowCommand
        {
            get
            {
                return new RelayCommand(delegate
                {
                    Messenger.Default.Send<UpdateRouteViewCommand>(new UpdateRouteViewCommand(this));
                    if(!InnerValue.IsSearch)Messenger.Default.Send<UpdateContextCommand>(new UpdateContextCommand()
                    {
                        Action = UpdateContextCommandType.SORT,
                        Argument = this.InnerValue                
                    });
                });
            }
        }
        public RelayCommand PinEntryCommand
        {
            get
            {
                return new RelayCommand(delegate
                    {
                        StandardTileData tileData = new StandardTileData()
                        {
                            BackgroundImage = new Uri("/Background.png", UriKind.Relative),
                            Title = this.InnerValue.Title,
                            Count = 0,
#if UNSTABLE
                            BackTitle = this.InnerValue.Title
#endif
                        };
#if UNSTABLE
                        if (Settings.Current.EnableBackgroundAgent)
                        {
                            tileData.BackContent = (new DataAccess(this.InnerValue)).NextUp;
                        }
#endif
                        try
                        {
                            ShellTile.Create(new Uri("/RoutePage.xaml?start=" +
                                this.InnerValue.startStation.id.ToString() + "&end=" +
                                this.InnerValue.endStation.id.ToString(), UriKind.Relative), tileData);
                        }
                        catch (InvalidOperationException)
                        {
                            MessageBox.Show(GeneralResource.RouteAlreadyPinnedString);
                        }
                    });
            }
        }
        public String NextUp
        {
            get { throw new NotImplementedException(); }
        }
        #endregion
        public RouteEntryWrapper(RouteEntry entry)
        {
            InnerValue = entry;
            Messenger.Default.Register<UpdateTick>(this, delegate
            {
                DispatcherHelper.CheckBeginInvokeOnUI(delegate
                {
                    RaisePropertyChanged("NextUp");
                });
            });
        }
    }
    public class DataItemWrapper : ViewModelBase
    {
        #region Binded properties
        public DataItem Value
        { get; private set; }
        public String DepartureTimeString
        {
            get
            {
                var DeparureTime = Value.Departure;
                return DeparureTime.ToString("t");
            }
        }
        public String ArrivalTimeString
        {
            get
            {
                var ArrivalTime = Value.Arrival;
                return ArrivalTime.ToString("t");
            }
        }
        public String DepartsInString
        {
            get
            {
                String _return = String.Empty;
                TimeSpan offset;
                //// If departed - say so
                if (Value.Departure < DateTime.Now) _return = ScheduleMVVM.Localization.GeneralResource.DepartedString;
                else
                {
                    offset = Value.Departure - DateTime.Now;
                    ControlResources.Culture = System.Threading.Thread.CurrentThread.CurrentUICulture;
                    var Minutes = offset.Minutes;
                    var Hours = offset.Hours;
                    //// Less than an hour
                    _return += ScheduleMVVM.Localization.GeneralResource.InString;
                    if (Hours != 0)
                    {
                        _return += Hours.ToString() + " ";
                        if (Hours / 10 == 1) _return += GeneralResource.XHoursOther+ " ";
                        else
                            switch (Hours % 10)
                            {
                                case 1:
                                    _return += GeneralResource.XHoursEnd1not11 + " ";
                                    break;
                                case 2:
                                case 3:
                                case 4:
                                    _return += GeneralResource.XHoursEnd2_4not12_14 + " ";
                                    break;
                                default:
                                    _return += GeneralResource.XHoursOther + " ";
                                    break;
                            }
                    }
                    if (Minutes != 0)
                    {
                        _return += Minutes.ToString() + " ";
                        if (Minutes / 10 == 1) _return += GeneralResource.XMinutesOther + " ";
                        else
                            switch (Minutes % 10)
                            {
                                case 1:
                                    _return += GeneralResource.XMinutesEnd1not11 + " ";
                                    break;
                                case 2:
                                case 3:
                                case 4:
                                    _return += GeneralResource.XMinutesEnd2_4not12_14 + " ";
                                    break;
                                default:
                                    _return += GeneralResource.XMinutesOther + " ";
                                    break;
                            }
                    }
                    if (Minutes == 0 && Hours == 0) _return = ScheduleMVVM.Localization.GeneralResource.AlmostDepartedString;
                }
                return _return;
            }
        }
        public String RouteString
        {
            get
            {
                return Value.RouteFromString + " - " + Value.RouteToString;
            }
        }
        public Boolean Actuality
        {
            get
            {
                return (Value.Departure - DateTime.Now).TotalMinutes < Settings.Current.RelevanceTimeout;
            }
        }
        public Brush FocusColor
        {
            get
            {
                if (Actuality) return (Brush)App.Current.Resources["PhoneAccentBrush"];
                else return (Brush)App.Current.Resources["PhoneForegroundBrush"];
            }
        }
        #endregion
        public DataItemWrapper(DataItem value)
        {
            Value = value;
            Messenger.Default.Register<UpdateTick>(this,delegate
            {
                DispatcherHelper.CheckBeginInvokeOnUI(delegate
                {
                    RaisePropertyChanged("DepartsInString");
                });
            });
        }
    }
}
